package org.hot100_1;

import java.util.HashSet;

public class leetcode22 {
    class ListNode{
        int val;
        ListNode next;
        ListNode (int val,ListNode next){
            this.val=val;
            this.next=next;
        }
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        HashSet<ListNode> set =new HashSet<>();

        ListNode temp = headA;

        while (temp!=null){
            set.add(temp);
            temp=temp.next;
        }

        temp =headB;

        while (temp!=null){
            if (set.contains(temp)){
                return temp;
            }
            temp=temp.next;
        }

        return null;
    }
}
